// 【collect 常用收藏】
const j = require('../../src/lib/collect')

describe('getType 获取数据类型', () => {
    test('null', () => {
        let res = j.getType(null)
        expect(res).toBe('null');
    });
    test('数组', () => {
        let res = j.getType([1, 2, 3])
        expect(res).toBe('array');
    });
})

describe('humpName 连字符转驼峰名', () => {
    test('标识符在第一个', () => {
        let res = j.humpName('-aaabbb')
        expect(res).toBe('aaabbb');
    });
    test('get-list', () => {
        let res = j.humpName('get-list')
        expect(res).toBe('getList');
    });
    test('get_list', () => {
        let res = j.humpName('get_list', '_')
        expect(res).toBe('getList');
    });
})

describe('toLowerLine 驼峰名转连字符', () => {
    test('标识符在第一个', () => {
        let res = j.toLowerLine('Aaabbb')
        expect(res).toBe('aaabbb');
    });
    test('getList', () => {
        let res = j.toLowerLine('getList')
        expect(res).toBe('get-list');
    });
    test('getList', () => {
        let res = j.toLowerLine('getList', '_')
        expect(res).toBe('get_list');
    });
})

describe('arrayRandom 数组随机排序', () => {
    test('简单数组', () => {
        let res = j.arrayRandom([1, 2, 3])
        expect(res).toHaveLength(3);
    });
    test('对象数组', () => {
        let res = j.arrayRandom([{ a: 1 }, { b: 2 }, { c: 3 }])
        expect(res).toHaveLength(3);
    });
})

describe('arrayChunk 数组等距拆分', () => {
    let list = [1, 2, 3, 4, 5]
    test('边界判断', () => {
        let res = j.arrayChunk(list, 9)
        expect(res).toHaveLength(1);
    });
    test('常规操作', () => {
        let res = j.arrayChunk(list, 3)
        expect(res).toHaveLength(2);
        expect(res[0]).toHaveLength(3);
        expect(res[1]).toHaveLength(2);
    });
})

describe('deepCloneJson 对象深拷贝【简易版】不拷贝对象里面的函数', () => {
    test('拷贝对象', () => {
        let obj = { name: 'tom', data: { key: 'a', value: 1 } }
        let objCopy = j.deepCloneJson(obj)
        objCopy.data = null
        expect(obj.name).toBe('tom');
        expect(obj.data.value).toBe(1);
    });
})

describe('debounce 函数防抖', () => {
    const sleep = (t) => { return new Promise(a => setTimeout(a, t)) }
    function debounceTest(title, immediate, time, value) {
        test(title, async () => {
            let count = 0
            // 防抖间隔 100ms
            const setCount = j.debounce(function () {
                count++
            }, 100, immediate)
            for (let i = 0; i < 3; i++) {
                await sleep(time)
                setCount()
            }
            expect(count).toBe(value);
        });
    }
    debounceTest('普通点击-立即执行', true, 200, 3)
    debounceTest('防抖操作-立即执行', true, 50, 1)
    debounceTest('普通点击-非立即执行', false, 200, 2)
    debounceTest('防抖操作-非立即执行', false, 50, 0)
})

describe('prefixNum 数字补零', () => {
    test('非空判断', () => {
        let res = j.prefixNum(NaN)
        expect(res).toBe('');
    });
    test('边界判断', () => {
        let res = j.prefixNum(100, 2)
        expect(res).toBe('100');
    });
    test('常规操作', () => {
        let res = j.prefixNum(2, 3)
        expect(res).toBe('002');
    });
})

describe('compareVersion 软件版本比较', () => {
    test('非空判断', () => {
        let res = j.compareVersion('', '')
        expect(res).toBe(false);
    });
    test('常规操作 - 没有新版本', () => {
        let res = j.compareVersion('1.0.1', '1.1.0')
        expect(res).toBe(false);
    });
    test('常规操作 - 有新版本', () => {
        let res = j.compareVersion('2.0.0', '1.9.99')
        expect(res).toBe(true);
    });
    test('常规操作 - 版本相同', () => {
        let res = j.compareVersion('1.0.0', '1.0.0')
        expect(res).toBe(false);
    });
})
